/**
 * Created by zhaojm on 15/3/17.
 */
//
game.Hero = cc.Node.extend({

    state_enum : {
        idle:0,
        jump:1
    },

    state : null,

    gravity : null,

    speedY : null,  // 当前的速度

    currentPos:null,

    _heroSprite:null,


    isCollided:null,

    isCanMove:null,
    isGuideTask : null,


    ctor:function(){
        this._super();
        //cc.log('frog ------ctor');
        this.isCollided = false;
        this.isCanMove = true;
        this.isGuideTask = -1;

        //this.setScale(0.5);


        //this._debugDrawNode = cc.DrawNode.create();
        //this.addChild(this._debugDrawNode, 10);




        this.idle();    // 静止

        this._heroSprite = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame(game.Config.fishRes.idle[0]));
        this._heroSprite.setAnchorPoint(cc.p(0, 0));
        this._heroSprite.setPosition(cc.p(0, 0));




        this.addChild(this._heroSprite);
        this.setContentSize(this._heroSprite.getContentSize());


        this.setCurrentPosition(0, 0);

        this.setAnchorPoint(cc.p(0.5, 0.5));

        this.setPosition(cc.p(0, 0));

        this.createAnimation();

        this.runAnimation('idle');

        this.scheduleUpdate();

    },

    runAnimation : function(action){
        var animation = cc.animationCache.getAnimation(action);
        var animate = cc.Animate.create(animation);
        this._heroSprite.stopAllActions();
        this._heroSprite.runAction(cc.RepeatForever.create(animate));
    },

    //runAction : function(action){
    //    this._heroSprite.runAction(action);
    //},

    //stopAllActions:function(){
    //    this._super();
    //    this._heroSprite.stopAllActions();
    //},


    jump:function(){
        if(this.getPositionY() >= game.Config.walter_height_percent * cc.winSize.height){
            return;
        }
        this.state = this.state_enum.jump;
        this.speedY = game.Config.hero_jump_speed_y;
        this.gravity = game.Config.hero_gravity;
        //this.touchCount++;
    },

    idle:function(){
        this.state = this.state_enum.idle;
        this.speedY = 0;
        this.gravity = 0;
        //this.touchCount = 0;
    },


    createAnimation:function(){
        var frames = [];
        var frameNames = game.Config.fishRes.idle;
        for(var i = 0; i < frameNames.length; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame(frameNames[i]);
            frames.push(frame);
        }

        var anim  = cc.Animation.create(frames, 0.2);
        cc.animationCache.addAnimation(anim, "idle");


        var frames = [];
        var frameNames = game.Config.fishRes.collided;
        for(var i = 0; i < frameNames.length; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame(frameNames[i]);
            frames.push(frame);
        }
        var anim  = cc.Animation.create(frames, 0.2);
        cc.animationCache.addAnimation(anim, "collided");
    },

    setCurrentPosition:function(x, y){

        this.currentPos = cc.p(x, y);
    },
    setCurrentPosition : function(pos){
        this.currentPos = pos;
    },

    setCurrentPositionX:function(x){

        this.currentPos.x = x;
    },

    setCurrentPositionY:function(y){

        this.currentPos.y = y;
    },

    update:function(dt){
        this._super();
        if(!this.isCanMove){
            return;
        }

        if(!this.isCollided){
            this.currentPos.y = 1 / 2 * this.gravity * dt * dt + this.speedY * dt + this.currentPos.y;
            this.speedY = this.speedY + this.gravity * dt ;

            if(this.currentPos.y <= cc.winSize.height * game.Config.hero_default_y_percent){
                this.currentPos.y = cc.winSize.height * game.Config.hero_default_y_percent;
                this.idle();
            }

            var a = Math.atan(this.speedY/ 100 / this.parent.speed) * 180 / Math.PI;
            this.setRotation(-a);

        }

        this.currentPos.x += this.parent.speed;


        this.setPosition(this.currentPos);


        this.parent.distance = Math.floor(this.currentPos.x);
        this.parent.uiLayer.setDistance(Math.floor(this.currentPos.x / 10));

        //if(this.isGuideTask == 0){
        //    if(this.speedY < 0 && this.currentPos.y < game.Config.walter_height_percent * cc.winSize.height){
        //        // 在向下走，并 进入水中
        //        this.parent.beginGuide2();
        //    }
        //}else if(this.isGuideTask == 1){
        //    if(this.currentPos.y <= cc.winSize.height * game.Config.walter_height_percent){
        //        this.parent.beginGuide3();
        //    }
        //}else if(this.isGuideTask == -1){
        //
        //}


    },







});

game.Hero.create = function(){
    return new game.Hero();
};